package com.yy.ctrl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.yy.bean.ConsultContent;
import com.yy.service.CommonService;

@Controller
public class StevenCtrl {

	@Value("${application.name: DouBi}")
	private String name = "";
	
	/**
	 * logger日志
	 */
	private Logger logger = LoggerFactory.getLogger(StevenCtrl.class);
	
	@Autowired
	private CommonService commonService;
	
	/**
	 * JDBC方式访问数据库
	 * @return
	 */
	@RequestMapping("/queryContent")
	@ResponseBody
    public List<ConsultContent> queryContent() {
        logger.info("JDBC --- 查询开始！");
        List<ConsultContent> consults = commonService.queryContent(new HashMap());
        logger.info("JDBC --- 查询结束！");
        return consults;
    }
	
	/**
	 * freemarker模板;
	 * 如果在application.yml或者application.properties中没有配置freemarker路径.则默认访问resource下的templates文件夹下的对应名称的.ftl文件;
	 * 如果设置了路径.则访问路径下的对应名称的.ftl文件
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/freemarker")
	public String freemarker(HttpServletResponse response, Map<String, Object> map) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		map.put("name", "[Angel -- 守护天使]");
        map.put("gender", 1);//gender:性别，1：男；0：女；  
        
        List<Map<String, Object>> friends = new ArrayList<Map<String, Object>>();
        Map<String, Object> friend = new HashMap<String, Object>();
        friend.put("name", "jack");
        friend.put("age", 30);
        friends.add(friend);
        friend = new HashMap<String, Object>();
        friend.put("name", "jeff");
        friend.put("age", 36);
        friends.add(friend);
        map.put("friends", friends);
		
		return "freemarker";
	}
	
	@RequestMapping(value = "/login")
	@ResponseBody
	public String login(String args) {
		return "Hello: " + (StringUtils.isEmpty(args) ? this.name : args);
	}
	
	/**
	 * 两个url都可以访问到此方法
	 * @param map
	 * @return
	 */
	@RequestMapping(value = {"/", "/index"})
	public String index(Map<String, Object> map) {
		map.put("name", this.name);
		map.put("time", new Date());
		
		return "index";
	}
	
	@RequestMapping(value = "/page")
	public ModelAndView page() {
		ModelAndView model = new ModelAndView();
		model.setViewName("index");
		model.addObject("name", this.name);
		model.addObject("time", new Date());
		
		return model;
	}
	
}
